package com.wzt.lianfirecontrol.lecheng;

import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.ksyun.media.player.IMediaPlayer;
import com.ksyun.media.player.KSYMediaPlayer;
import com.wzt.lianfirecontrol.R;
import com.wzt.lianfirecontrol.activity.BaseActivity;

import java.io.IOException;

public class PlayVideoActivity extends BaseActivity {

    private Bundle bundle;

    private View include_head_title;
    private ImageView iv_back;
    public TextView tv_title;

    private String url;

    // 播放器的对象
    private KSYMediaPlayer ksyMediaPlayer;
    // SurfaceView需在Layout中定义，此处不在赘述
    private SurfaceView mVideoSurfaceView;
    private SurfaceHolder mSurfaceHolder;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        bundle = getIntent().getExtras();
        if (null != bundle){
            url = bundle.getString("url");
        }
        setContentView(R.layout.a_play_video);
        setImmerseLayout(findViewById(R.id.include_head_title), findViewById(R.id.rl_head_content));
        initHeadTitle();
        init();
    }

    private void initHeadTitle() {
        include_head_title = findViewById(R.id.include_head_title);
        include_head_title.setBackground(getResources().getDrawable(R.drawable.shape_head_title_bg_frame));
        iv_back = findViewById(R.id.iv_back);
        iv_back.setImageResource(R.drawable.back_white);
        iv_back.setVisibility(View.VISIBLE);
        tv_title = findViewById(R.id.tv_title);
        tv_title.setTextColor(getResources().getColor(R.color.white));
        tv_title.setText("视频监控");

        findViewById(R.id.v_divdier_line).setVisibility(View.GONE);

    }

    private void init() {
        ksyMediaPlayer = new KSYMediaPlayer.Builder(this.getApplicationContext()).build();

        ksyMediaPlayer.setOnCompletionListener(new IMediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(IMediaPlayer iMediaPlayer) {
                // 播放完成时会发出onCompletion回调

            }
        });
        ksyMediaPlayer.setOnPreparedListener(new IMediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(IMediaPlayer iMediaPlayer) {
                // 播放器在准备完成，可以开播时会发出onPrepared回调
                if(ksyMediaPlayer != null) {
                    // 设置视频伸缩模式
                    ksyMediaPlayer.setVideoScalingMode(KSYMediaPlayer.VIDEO_SCALING_MODE_SCALE_TO_FIT);
                    // 开始播放视频
                    ksyMediaPlayer.start();
                }
            }
        });
        ksyMediaPlayer.setOnInfoListener(new IMediaPlayer.OnInfoListener() {
            @Override
            public boolean onInfo(IMediaPlayer iMediaPlayer, int i, int i1) {
                //消息监听器,会将关于播放器的消息告知开发者,例如:视频渲染、音频渲染等
                return false;
            }
        });
        ksyMediaPlayer.setOnVideoSizeChangedListener(new IMediaPlayer.OnVideoSizeChangedListener() {
            @Override
            public void onVideoSizeChanged(IMediaPlayer iMediaPlayer, int i, int i1, int i2, int i3) {
                // 视频的宽高发生变化时会有相应的回调
            }
        });
        ksyMediaPlayer.setOnErrorListener(new IMediaPlayer.OnErrorListener() {
            @Override
            public boolean onError(IMediaPlayer iMediaPlayer, int i, int i1) {
                // 播放器遇到错误时会发出onError回调
                return false;
            }
        });
        ksyMediaPlayer.setOnSeekCompleteListener(new IMediaPlayer.OnSeekCompleteListener() {
            @Override
            public void onSeekComplete(IMediaPlayer iMediaPlayer) {
                //在seek成功后会发出此回调
            }
        });


        try {
            ksyMediaPlayer.setDataSource(url);
            ksyMediaPlayer.prepareAsync();
        } catch (IOException e) {
            e.printStackTrace();
        }

        mVideoSurfaceView = findViewById(R.id.preview_view);
        mSurfaceHolder = mVideoSurfaceView.getHolder();
        mSurfaceHolder.addCallback(mSurfaceCallback);
    }

    private final SurfaceHolder.Callback mSurfaceCallback = new SurfaceHolder.Callback() {
        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

        }

        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            if(ksyMediaPlayer != null) {
                ksyMediaPlayer.setDisplay(holder);
                ksyMediaPlayer.setScreenOnWhilePlaying(true);
            }
        }

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            // 此处非常重要，必须调用!!!
            if(ksyMediaPlayer != null) {
                ksyMediaPlayer.setDisplay(null);
            }
        }
    };

    @Override
    protected void onResume() {
        super.onResume();
        if(ksyMediaPlayer != null)
            ksyMediaPlayer.start();
    }

    @Override
    protected void onPause() {
        super.onPause();
        if(ksyMediaPlayer != null)
            ksyMediaPlayer.pause();
    }

    @Override
    protected void onStop() {
        super.onStop();
        // 在onStop时释放掉播放器
        if(ksyMediaPlayer != null)
            ksyMediaPlayer.release();
        ksyMediaPlayer = null;
    }

//    @Override
//    public void onBackPressed() {
//        // 在全屏或者小窗口时按返回键要先退出全屏或小窗口，
//        // 所以在Activity中onBackPress要交给NiceVideoPlayer先处理。
//
//    }
}
